 //控制层 
app.controller('itemCatController' ,function($scope,$controller,itemCatService,typeTemplateService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		itemCatService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		itemCatService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		itemCatService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=itemCatService.update( $scope.entity ); //修改  
		}else{
			serviceObject=itemCatService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询
					var id = $scope.breadcrumb[$scope.breadcrumb.length-1].id;
		        	$scope.findByParentId(id)//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){
		//获取选中的复选框			
		itemCatService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					//重新查询
					var id = $scope.breadcrumb[$scope.breadcrumb.length-1].id;
					$scope.findByParentId(id)//重新加载
					$scope.selectIds=[];
				}else {
					alert(response.message);
				}
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){
		itemCatService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

	/***********************************************************************************************/

	//定义实体类
	$scope.entity= {};

	//定义下拉选项
	$scope.optionsTypeTm= {
		data: []
	}

	//初始化时查询下拉多选列表
	$scope.getOptionsTypeTm = function() {

		typeTemplateService.getOptions().success(function (response) {
			$scope.optionsTypeTm.data=response;
		});

	}

	//面包屑数组定义
	$scope.breadcrumb = [{id:0,name:'顶级分类列表'}];

	//每次点击新建获取上级分类
	$scope.getBreadStr = function() {
		//清空原有数据
		$scope.entity={};
		//用于显示
		$scope.breadcrumbStr = $scope.breadcrumb.map(bra => bra.name ).join('>>');
		//获取数组最后一个元素用于赋值pid
		$scope.entity.parentId = $scope.breadcrumb[$scope.breadcrumb.length-1].id;
	}

	//根据父id查询
	$scope.findByParentId=function(parentId){
		itemCatService.findByParentId(parentId).success(
			function(response){
				$scope.list=response;
			}
		);
	}

	//点击下级,存储面包屑
	$scope.clickNext = function(id,name) {

			//存储面包屑
			$scope.breadcrumb.push({id: id,name:name});

			//调用查询
			$scope.findByParentId(id);

	}

	//点击面包屑
	$scope.clickBreadcrumb = function (id,index) {

		//清空后面的面包屑  ps:根据下一位的下标，清空两个（最多两个）
		$scope.breadcrumb.splice(index+1,2)

		//调用查询
		$scope.findByParentId(id);
	}

	//定义数组存放模板名
	$scope.templateList = [];

	//商品模板信息查询
	$scope.findAllTemplate = function () {

		typeTemplateService.findAll().success(function (response) {

			for (let i in response) {
				$scope.templateList[response[i].id] = response[i].name;
			}

		});

	}
});	
